package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.PermissionConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.ResetPasswordDTO;
import com.sky.dto.UserInfoUpdateDTO;
import com.sky.dto.UserLoginDTO;
import com.sky.dto.UserRegisterDTO;
import com.sky.entity.User;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.UserMapper;
import com.sky.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author 贾晓晨
 * @version 1.0
 */
@Slf4j
@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        //1、根据用户名查询数据库中的数据
        User user = userMapper.getByUsername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // 后期需要进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (user.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }


        //3、返回实体对象
        return user;
    }

    @Override
    public void register(UserRegisterDTO userRegisterDTO) {
        String pwd = userRegisterDTO.getPassword().getBytes() != null ?
                    userRegisterDTO.getPassword() : PasswordConstant.DEFAULT_PASSWORD;
        userMapper.register(userRegisterDTO,DigestUtils.md5DigestAsHex(pwd.getBytes()));
    }

    @Override
    public User isUsernameExists(String username) {
        return userMapper.isUsernameExist(username);
    }

    @Override
    public User isEmailExists(String email) {
        return userMapper.isEmailExists(email);
    }

    @Override
    public void resetPassword(ResetPasswordDTO resetPasswordDTO) {
        String md5Pwd = DigestUtils.md5DigestAsHex(resetPasswordDTO.getPassword().getBytes());
        String email = resetPasswordDTO.getEmail();
        userMapper.resetPassword(email,md5Pwd);
    }

    @Override
    public void updateInfo(UserInfoUpdateDTO userInfoUpdateDTO) {
        userMapper.updateInfo(userInfoUpdateDTO);
    }

    @Override
    public String getAvatar() {
        return userMapper.getAvatar();
    }

}
